﻿using System;
using System.Linq;
using Renci.Data.Interop.OpenGIS.SensorML_101;
using Renci.Data.Interop.OpenGIS.Sos_10;

namespace Renci.Ogc.Client
{
    public class Program
    {
        /// <remarks/>
        static void Main(string[] args)
        {
            GetCapabilities c = new GetCapabilities();
            c.UpdateSequence = "XY123";
            c.AcceptVersions = new string[] { "1.0.0", "0.8.3" };
            c.AcceptFormats = new string[] { "text/xml" };

            string url = "http://localhost:2693/Swe.svc/";
            //string url = "http://sdf.ndbc.noaa.gov/sos/server.php";

            var client = new SosClient(new Uri(url), "1.0.0");

            var capabilities = client.GetCapabilities();

            var offerings = from o in capabilities.Contents.ObservationOfferings
                             select o;

            foreach (var offering in offerings)
            {
                Console.WriteLine("Offering:" + offering.Names[0]);
                var sensor = client.DescribeSensor(new DescribeSensor
                {
                    OutputFormat = "text/xml;subtype=\"sensorML/1.0.0\"",
                    Procedure = offering.Procedures[0].Href,
                    Version = "1.0.0",
                });
                Console.WriteLine("Sensor:" + ((SystemType)sensor.Members[0].Item).Id);

                var observationRequest = new GetObservation
                {
                    FeatureOfInterest = new GetObservationFeatureOfInterest
                    {
                        ObjectId = offering.FeaturesOfInterest[0].Href,
                    },
                    Offering = offering.Names[0],
                    ResponseFormat = "text/xml",
                    Version = "1.0.0",                    
                };

                observationRequest.ObservedProperties.Add(offering.ObservedProperties[0].Href);

                var observations = client.GetObservation(observationRequest);
                Console.WriteLine("\tTotal Observations :" + observations.Members.Count);
                foreach (var observation in observations.Members)
                {
                    Console.WriteLine("\t\tObservation:" + observation.Observation.Id);
                }
            }
        }
    }
}
